<?php
if(!interface_exists('XTestCase', false)){
	require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'XTestCase.php');
}
abstract class XAbstractTestCase implements XTestCase{
	protected $tests = array();
	protected $countTestCases = 0;
	protected $passedTests = array();
	protected $failedTests = array();
	
	public function __construct(){
		$methods = XClassLoader::loader(get_class($this))->reflection()->getMethods();
		foreach($methods as $method){
			$name = $method->getName();
			if(!preg_match("/^test[A-Z]/i", $method->getName())){
				continue;
			}
			$this->tests[$name] = $method;
		}
		$this->countTestCases = count($this->tests);
	}
	
	public function countTestCases(){
		return $this->countTestCases;
	}
		
	public function run(){
		$this->setUp();
		foreach($this->tests as $key => $value){
			$this->runTest($key);
		}
		$this->tearDown();
		$this->report();
	}
	
	protected function runTest($method){
		try{			
			$this->$method();	
			$this->passedTests[$method] = $method;		
		}catch(XAssertFailedException $e){
			$this->failedTests[$method] = $e;		
		}catch(Exception $e){
			throw $e;
		}
	}
	
	protected function setUp(){
		
	}

	protected function tearDown(){
		
	}
	
	protected function report(){
		p(count($this->tests)." Tests runned, ".count($this->passedTests)." passed, ".count($this->failedTests)." failed.");
		foreach($this->tests as $key => $method){
			if(isset($this->passedTests[$key])){
				p("{$key}: passed.");
				continue;
			}
			if(isset($this->failedTests[$key])){
				p("{$key}: failed.");
				p($this->failedTests[$key]->getMessage());
			}
		}
		p(count($this->tests)." Tests runned, ".count($this->passedTests)." passed, ".count($this->failedTests)." failed.");
	}
}
?>